package io.renren.modules.hos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.renren.modules.hos.entity.DoctorEntity;
import io.renren.modules.hos.entity.PatientEntity;
import io.renren.modules.hos.form.OperationForm;
import io.renren.modules.hos.service.DoctorService;
import io.renren.modules.hos.service.PatientService;
import io.renren.modules.hos.vo.DoctorOperationVo;
import io.renren.modules.hos.vo.PatientOperationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.hos.dao.OperationDao;
import io.renren.modules.hos.entity.OperationEntity;
import io.renren.modules.hos.service.OperationService;


@Service("operationService")
public class OperationServiceImpl extends ServiceImpl<OperationDao, OperationEntity> implements OperationService {
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private PatientService patientService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OperationEntity> page = this.page(
                new Query<OperationEntity>().getPage(params),
                new QueryWrapper<OperationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryByPatientId(Long id, Map<String, Object> params) {
        LambdaQueryWrapper<OperationEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OperationEntity::getPatientId, id);
        List<OperationEntity> operationEntities = baseMapper.selectList(queryWrapper);
        int pageSize = (int) params.get("pageSize");
        int pageIndex = (int) params.get("pageIndex");
        List<PatientOperationVo> patientOperationVos = patientCopyList(operationEntities);
        return new PageUtils(patientOperationVos, patientOperationVos.size(), pageSize, pageIndex);
    }

    @Override
    public boolean addOperation(Long doctorId, OperationForm form) {
        OperationEntity operationEntity = new OperationEntity();
        operationEntity.setOperationTime(new Date());
        operationEntity.setDoctorId(doctorId);
        operationEntity.setPatientId(form.getPatientId());
        operationEntity.setOperationContent(form.getContent());

        return this.save(operationEntity);
    }

    @Override
    public PageUtils queryByDoctorId(Long doctorId, Map<String, Object> params) {
        LambdaQueryWrapper<OperationEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OperationEntity::getDoctorId, doctorId);
        int pageSize = (int) params.get("pageSize");
        int pageIndex = (int) params.get("pageIndex");
        List<OperationEntity> operationEntities = baseMapper.selectList(queryWrapper);
        List<DoctorOperationVo> doctorOperationVos = doctorCopyList(operationEntities);

        return new PageUtils(doctorOperationVos, doctorOperationVos.size(), pageSize, pageIndex);
    }

    public PatientOperationVo patientCopy(OperationEntity operationEntity){
        DoctorEntity doctorEntity = doctorService.getById(operationEntity.getDoctorId());
        PatientOperationVo patientOperationVo = new PatientOperationVo();
        patientOperationVo.setDoctorName(doctorEntity.getDoctorName());
        patientOperationVo.setOperationContent(operationEntity.getOperationContent());
        patientOperationVo.setOperationTime(operationEntity.getOperationTime());

        return patientOperationVo;
    }

    public List<PatientOperationVo> patientCopyList(List<OperationEntity> operationEntities){
        List<PatientOperationVo> patientOperationVos = new ArrayList<>();
        for (OperationEntity operationEntity : operationEntities) {
            patientOperationVos.add(patientCopy(operationEntity));
        }

        return patientOperationVos;
    }

    public DoctorOperationVo doctorCopy(OperationEntity operationEntity){
        PatientEntity patientEntity = patientService.getById(operationEntity.getPatientId());
        DoctorOperationVo doctorOperationVo = new DoctorOperationVo();
        doctorOperationVo.setPatientName(patientEntity.getPatientName());
        doctorOperationVo.setOperationTime(operationEntity.getOperationTime());
        doctorOperationVo.setOperationContent(operationEntity.getOperationContent());

        return doctorOperationVo;
    }

    public List<DoctorOperationVo> doctorCopyList(List<OperationEntity> operationEntities){
        List<DoctorOperationVo> doctorOperationVos = new ArrayList<>();
        for (OperationEntity operationEntity : operationEntities) {
            doctorOperationVos.add(doctorCopy(operationEntity));
        }
        return doctorOperationVos;
    }
}